package collection;

import java.lang.reflect.Array;
import java.util.*;

public class Exercise {
    public static void main(String[] args) {
/*        Collection c = new ArrayList();
        c.add(new Point(1,2));
        c.add(new Point(3,4));
        c.add(new Point(9,8));
        c.add(new Point(0,0));
        System.out.println(c);
        System.out.println("集合c的元素个数是："+c.size());//4
        System.out.println("集合c是否为空集："+c.isEmpty());//false
        //c.clear();//清空集合
        Point p = new Point(1,2);
        System.out.println("集合c是否包含p："+c.contains(p));//true 判断包含用contains()
        System.out.println(c.remove(new Point(9,8)));//成功删除返回true
        System.out.println(c.remove(new Point(3,4)));
        System.out.println(c);

        Collection<String> c1 = new ArrayList();
        c1.add("java");
        c1.add("php");
        c1.add("android");
        c1.add("c++");
        c1.add(".net");
        System.out.println(c1);
        c.addAll(c1);
        System.out.println(c);
        System.out.println(c.containsAll(c1));//ture
        System.out.println(c.retainAll(c1));//取交集 c:[(1,2), (0,0), java, php, android, c++, .net]
                                            //      c1:[java, php, android, c++, .net]
        System.out.println(c);              //       c:[java, php, android, c++, .net]
        System.out.println(c.removeAll(c1));//删交集
                                            //       c:[java, php, android, c++, .net]
                                            //      c1:[java, php, android, c++, .net]
        System.out.println(c);              //       c:[]
        */
        //迭代器
        /*        Collection c = new ArrayList();
        c.add("one");
        c.add("#");
        c.add("two");
        c.add("#");
        c.add("three");
        c.add("#");
        c.add("four");
        c.add("#");
        c.add("five");
        c.add("#");
        System.out.println(c);
        Iterator it = c.iterator();
        while (it.hasNext()){
            String str = (String) it.next();
            System.out.println(str);
            if("#".equals(str)){
                it.remove();
            }

        }
        System.out.println(c);//[one, two, three, four, five]*/
        //增强for循环（新循环）
        /*String[] str = {"房子着火我拍照", "人生乱套我睡觉", "大难临头不知道","大不了我就上吊", "今天的事儿能拖就拖",
                "明天的事儿明天再说","我就乐意这样活"};
        for(String altitude:str){
            System.out.println(altitude);
        }
        System.out.println("-------------------------");
        Collection<String> c = new ArrayList<>();
        c.add("门前大桥下");
        c.add("游过一群鸭");
        c.add("快来快来数一数");
        c.add("二四六七八");
        c.add("嘎嘎嘎嘎");
        c.add("真呀真多呀");
        System.out.println(c);

        for (Object count:c){
            System.out.println(count);
        }*/
   //泛型
       /* Collection<Point> c = new ArrayList<>();
        c.add(new Point(111,222));
        c.add(new Point(44,86));
        c.add(new Point(75,13));
        c.add(new Point(1,2));
        c.add(new Point(5,6));
        c.add(new Point(741,33));
        System.out.println(c);
        Iterator<Point> it = c.iterator();
        while (it.hasNext()){
            Point point = (Point) it.next();
            System.out.println(point);
        }
        for(Object o:c){//Object是元素类型 o是数组中每个元素 c是要迭代的集合
            System.out.println(o);
        }*/
       //集合转换为数组
       Collection<Integer> i = new ArrayList<>();
       i.add(1);
       i.add(2);
       i.add(3);
       i.add(4);
       i.add(5);
        System.out.println("集合i为："+i);
        Integer[] array = i.toArray(new Integer[6]);
        System.out.println("数组array为："+Arrays.toString(array));

        //数组转换为集合
        Integer[] integers ={1,2,3,151,8361,7,8,9};
        System.out.println("数组integers为："+Arrays.toString(integers));
        List<Integer> list = Arrays.asList(integers);
        System.out.println("integers集合为:"+list);

        integers[1] = 0;
        System.out.println("数组integers为："+Arrays.toString(integers));
        System.out.println("integers集合为:"+list);

        list.set(2,0);
        System.out.println("数组integers为："+Arrays.toString(integers));
        System.out.println("integers集合为:"+list);

        list.add(5);//UnsupportedOperationException 运行时会发生不支持操作异常
    }
}
